/**
 *******************************************************************************
 * @file  template/source/main.c
 * @brief Main program template for the Device Driver Library.
 @verbatim
   Change Logs:
   Date             Author          Notes
   2022-03-31       CDT             First version
 @endverbatim
 *******************************************************************************
 * Copyright (C) 2022-2025, Xiaohua Semiconductor Co., Ltd. All rights reserved.
 *
 * This software component is licensed by XHSC under BSD 3-Clause license
 * (the "License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                    opensource.org/licenses/BSD-3-Clause
 *
 *******************************************************************************
 */

/*******************************************************************************
 * Include files
 ******************************************************************************/
#include "main.h"

#include "FreeRTOS.h"
#include "task.h"

#include "usbh_core.h"

#include "drv_sdio.h"
#include "drv_lcd.h"
#include "drv_spi.h"

#include "lcd.h"
#include "pic.h"

#define LED_TOGGLE_PERIOD (100)

/**
 * @addtogroup HC32F4A0_DDL_Examples
 * @{
 */

/**
 * @addtogroup LL_Templates
 * @{
 */

/*******************************************************************************
 * Local type definitions ('typedef')
 ******************************************************************************/

/*******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/

/*******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/

/*******************************************************************************
 * Local function prototypes ('static')
 ******************************************************************************/

/*******************************************************************************
 * Local variable definitions ('static')
 ******************************************************************************/

/*******************************************************************************
 * Function implementation - global ('extern') and local ('static')
 ******************************************************************************/

extern void cdc_acm_init(uint8_t busid, uintptr_t reg_base);
extern void cdc_acm_data_send_with_dtr_test(uint8_t busid);

extern void msc_ram_init(uint8_t busid, uintptr_t reg_base);

void led_task(void *param)
{
    while(1)
    {
        GPIO_TogglePins(BSP_LED0_PORT, BSP_LED0_PIN);
        vTaskDelay(pdMS_TO_TICKS(LED_TOGGLE_PERIOD));
        GPIO_TogglePins(BSP_LED1_PORT, BSP_LED1_PIN);
        vTaskDelay(pdMS_TO_TICKS(LED_TOGGLE_PERIOD));
        GPIO_TogglePins(BSP_LED2_PORT, BSP_LED2_PIN);
        vTaskDelay(pdMS_TO_TICKS(LED_TOGGLE_PERIOD));
        GPIO_TogglePins(BSP_LED3_PORT, BSP_LED3_PIN);
        vTaskDelay(pdMS_TO_TICKS(LED_TOGGLE_PERIOD));
        GPIO_TogglePins(BSP_LEDSIDE0_PORT, BSP_LEDSIDE0_PIN);
        vTaskDelay(pdMS_TO_TICKS(LED_TOGGLE_PERIOD));
        GPIO_TogglePins(BSP_LEDSIDE1_PORT, BSP_LEDSIDE1_PIN);
        vTaskDelay(pdMS_TO_TICKS(LED_TOGGLE_PERIOD));
    }
    vTaskDelete(NULL);
}

void lsusb_task(void *param)
{
//    char *args[] = {"lsusb", "-v"};
//    while(1)
//    {
//        lsusb(2, args);
//        vTaskDelay(pdMS_TO_TICKS(3000));
//    }
    vTaskDelete(NULL);
}

void lcd_task(void *param)
{
    SPI1_Init();
    LCD_Init();
    
    LCD_BLK_Set();

    while(1)
    {
        LCD_Fill(0, 0, LCD_W, LCD_H, BLACK);
        LCD_ShowPicture(80, 20, 80, 80, gImage_pic);
        vTaskDelay(pdMS_TO_TICKS(1000));
        LCD_Fill(0, 0, LCD_W, LCD_H, RED);
        vTaskDelay(pdMS_TO_TICKS(1000));
        LCD_Fill(0, 0, LCD_W, LCD_H, GREEN);
        vTaskDelay(pdMS_TO_TICKS(1000));
        LCD_Fill(0, 0, LCD_W, LCD_H, BLUE);
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
    vTaskDelete(NULL);
}

/**
 * @brief  Main function of template project
 * @param  None
 * @retval int32_t return value, if needed
 */
int32_t main(void)
{
    /* MCU Peripheral registers write unprotected. */
    LL_PERIPH_WE(LL_PERIPH_EFM | LL_PERIPH_FCG | LL_PERIPH_GPIO | LL_PERIPH_PWC_CLK_RMU | LL_PERIPH_SRAM);
    
    BSP_CLK_Init();
    
    /* Configure UART */
    DDL_PrintfInit(BSP_PRINTF_DEVICE, BSP_PRINTF_BAUDRATE, BSP_PRINTF_Preinit);
    
    BSP_LED_Init();
    BSP_LEDSide_Init();

    /* MCU Peripheral registers write protected. */
    LL_PERIPH_WP(LL_PERIPH_EFM | LL_PERIPH_SRAM);
    
//    cdc_acm_init(0, CM_USBFS_BASE);
//    SDIOC_Config();
//    msc_ram_init(0, CM_USBFS_BASE);
    usbh_initialize(0, CM_USBHS_BASE); 

    xTaskCreate(lsusb_task, "lsusb_task", 256, NULL, 0, NULL);
    xTaskCreate(led_task, "led_task", 128, NULL, 1, NULL);
    xTaskCreate(lcd_task, "lcd_task", 1024, NULL, 1, NULL);
    //xTaskCreate(sdio_task, "sdio_task", 1024, NULL, 3, NULL);
    vTaskStartScheduler();
    
    for (;;) {
    }
}

/**
 * @}
 */

/**
 * @}
 */

/*******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/
